package types

import (
	"context"
	"sync"
)

// 泛型并发安全 Map 实现

// Mkey 类型约束定义
type Mkey interface {
	string | int | uint // Key 允许类型：字符串、int、uint
}

type MValue interface {
	// Value 允许类型：
	*WsClient | *ChatSession | context.CancelFunc | []interface{}
}

// LMap 泛型 Map 结构
type LMap[K Mkey, T MValue] struct {
	lock sync.RWMutex
	data map[K]T
}

func NewLMap[K Mkey, T MValue]() *LMap[K, T] {
	return &LMap[K, T]{
		lock: sync.RWMutex{},
		data: make(map[K]T),
	}
}

func (m *LMap[K, T]) Put(key K, value T) {
	m.lock.Lock()
	defer m.lock.Unlock()
	m.data[key] = value
}

func (m *LMap[K, T]) Get(key K) T {
	m.lock.RLock()
	defer m.lock.RUnlock()
	return m.data[key]
}

func (m *LMap[K, T]) Has(key K) bool {
	m.lock.RLock()
	defer m.lock.RUnlock()
	_, ok := m.data[key]
	return ok
}

func (m *LMap[K, T]) Delete(key K) {
	m.lock.Lock()
	defer m.lock.Unlock()
	delete(m.data, key)
}

// ToList 把 value 转成切片返回
func (m *LMap[K, T]) ToList() []T {
	m.lock.Lock()
	defer m.lock.Unlock()

	var s = make([]T, 0)
	for _, v := range m.data {
		s = append(s, v)
	}
	return s
}
